package com.example.project.projectspringcloud.service.impl.sys;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.dao.sys.AreaMapper;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.sys.Area;
import com.example.project.projectspringcloud.service.BaseService;
import com.example.project.projectspringcloud.service.sys.SysAreaService;
import com.example.project.projectspringcloud.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysAreaServiceImpl extends BaseService<Area, AreaMapper> implements SysAreaService {

    @Autowired
    private MessageSource messageSource;

    @Override
    public List<Area> list(Area entity, Page page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }

        return super.mapper.selectAll(entity);
    }

    @Override
    public boolean changeShow(Area area) throws OperationException {
        Integer isOpen = area.getIsOpen();
        area = super.mapper.selectOne(area);
        if (area == null) {
            throw new OperationException(StringUtil.translateI18N(messageSource, Constants.Message.INFORMATION_DOES_NOT_EXIST));
        }
        area.setIsOpen(isOpen);
        if (super.mapper.updateByPrimaryKeySelective(area) > 0) {
            return true;
        }
        return false;
    }

}
